package com.sunmi.sbs.commons.utils;

import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class XorStringUtils {
    public static byte[] getSHA256Byte(String str){
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes(StandardCharsets.UTF_8));
            encdeStr = Hex.encodeHexString(hash);
            return hash;

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getSHA256Str(String str){
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes(StandardCharsets.UTF_8));
            encdeStr = Hex.encodeHexString(hash);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return encdeStr;
    }
    public static String fromCharCode(int i)
    {
        String strValue=""+(char)i;
        return strValue;
    }
    public static String xorString(String s1, String s2){
        int s1Len = s1.length();
        int s2Len = s2.length();
        int i ;
        String result ="";
        for (i=0;i< s1Len; i++){
//            System.out.println(s1.charAt(i));
//            System.out.println(s2.charAt(i%s2Len));
            result += fromCharCode(s1.charAt(i) ^ s2.charAt(i%s2Len));
//            result += s1.charAt(i) ^ s2.charAt(i%s2Len);

//            System.out.println(result);
        }
        return result;
    }
}
